import axios from "axios";

class WallpaperApi {

    GET_CATEGORY_URL = "http://wp.birdpaper.com.cn/intf/getCategory";
    GET_LIST_URL = "http://wp.birdpaper.com.cn/intf/GetListByCategory";
    SEARCH_LIST_URL = "http://wp.birdpaper.com.cn/intf/search";

    params = {
        cids: '',
        content: '',
        pageno: 1,
        count: 20
    }

    types = [{
        "category": "",
        "show_name": "",
        "icon": "",
        "position": "",
        "hot_tag": [{
            "tag": "",
            "show_tag": "",
            "icon": ""
        }],
        "old_id": ""
    }];

    data = {
        "total_count": 0,
        "total_page": 0,
        "pageno": 0,
        "count": 0,
        "list": []
    }

    getCategoryList() {
        return new Promise(resolve => {
            axios.get(this.GET_CATEGORY_URL).then(res => {
                if (res.data && res.data.errno === 0) {
                    resolve(res.data.data);
                } else {
                    resolve(null);
                }
            })
        });
    }

    getWallpaperList(pageIndex, categoryId, searchKey, count = 20) {
        return new Promise(resolve => {
            let url = this.GET_LIST_URL;
            const params = Object.assign({}, this.params);
            params.pageno = pageIndex;
            if (categoryId) {
                params.cids = categoryId;
            }
            if (searchKey) {
                params.content = searchKey;
                params.cids = '';
                url = this.SEARCH_LIST_URL;
            }
            axios.get(url, {
                params: params
            }).then(res => {
                if (res.data && res.data.errno === 0) {
                    resolve(res.data.data);
                } else {
                    resolve(null)
                }
            })
        });
    }
}

export default new WallpaperApi();
